package com.p2p.front.web.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.p2p.base.annotation.LoginNeedless;
import com.p2p.base.model.BaseUser;
import com.p2p.base.service.LoanStatusService;
import com.p2p.base.web.controller.BaseController;
import com.p2p.front.dal.model.ImageStore;
import com.p2p.front.dal.model.LoanInfo;
import com.p2p.front.dal.model.UserCredits;
import com.p2p.front.service.FinanceService;
import com.p2p.front.service.ImageStoreService;
import com.p2p.front.service.InvestInfoService;
import com.p2p.front.service.LoanInfoService;
import com.p2p.front.service.TenderAmountSumService;
import com.p2p.front.service.TenderAutoService;
import com.p2p.front.service.UserCreditsService;
import com.p2p.trans.client.facade.AccountQueryFacade;
import com.p2p.trans.client.model.AccountVO;
import com.p2p.trans.client.result.AccountQueryResult;

/**
 * @author ruiqi
 *
 */
@LoginNeedless
@Controller
public class IndexController extends BaseController {
	@Resource
	private LoanInfoService loanInfoService;

	@Resource
	private LoanStatusService constService;
	
	@Resource
	private InvestInfoService investInfoService;
	
	@Resource
	private TenderAutoService tenderAutoService;

	@Resource
	AccountQueryFacade remoteAccountQueryFacade;
	
	@Resource
	private FinanceService financeService;
	
	@Resource
	private ImageStoreService imageStoreService;
	
	@Resource
	private UserCreditsService userCreditsService;
	
	@Resource
	private TenderAmountSumService tenderAmountSumService;
	
/*	
	@RequestMapping("")
	public String welcome3(Model model) {
		return doWelcome(model);
	}*/
	
	@RequestMapping("/")
	public String welcome2(Model model) {
		return doWelcome(model);
	}
	
	@RequestMapping("/welcome/index")
	public String welcome(Model model) {
		return doWelcome(model);
	}

	@RequestMapping("/welcome/login_box")
	public String loginBox(Model model) {
		return "welcome/login_box";
	}
	
	private String doWelcome(Model model) {
		BaseUser user = getCurrentUser();
		model.addAttribute("user", user);
		if(user!=null){
			//得到首页中需要显示的投资信息
			int tenderNum = investInfoService.getAllNum(user.getId()); 
			model.addAttribute("tenderNum", tenderNum);
			int uid = Integer.valueOf(user.getId());
			//获取总的资金记录用于页面展示
			Map<String,Object> finance = financeService.myFinance(uid);
			//获得代收总额
			int toBackAmount = (int) finance.get("toBackAmount");
			model.addAttribute("toBackAmount", toBackAmount);
			
			boolean inAutoQueue =tenderAutoService.checkInAutoQueue(user.getId());
			
			model.addAttribute("inAutoQueue", inAutoQueue);
			
			AccountQueryResult accountQueryResult  = remoteAccountQueryFacade.getAccountByUserId(user.getId());
			if (accountQueryResult.isSuccess()) {
				AccountVO accountVO = accountQueryResult.getAccountVO();
				model.addAttribute("accountVO", accountVO);
			}

			
			
		}
		List<ImageStore> bannerList =imageStoreService.queryBizAndStatus(1L, 0, 1);
		
		model.addAttribute("bannerList", bannerList);
		List<LoanInfo> succeededLoans = loanInfoService.getLastSucceededLoan();
		model.addAttribute("succeededLoans", succeededLoans);
		
		List<Map<String,Object>> list = tenderAmountSumService.queryTopTotalAmount();
		model.addAttribute("topTen", list);
		return "welcome/index";
	}
	
	@LoginNeedless
	@RequestMapping("/topTotalRank")
	public void getTopTotalTenderAmount(HttpServletResponse response){
		List<Map<String,Object>> list = tenderAmountSumService.queryTopTotalAmount();
		this.returnJsonObject(response, list);
		return;
	}
}
